package com.xrx.webtemplate.shiro.service;

import com.xrx.webtemplate.model.ShiroSessionModel;
import com.xrx.webtemplate.web.api.TokenApi;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import java.io.Serializable;
import java.util.Collection;
@Component("sessionRepository")
public class ShiroSessionRepositoryImpl implements ShiroSessionRepository {

    private static final String REDIS_SHIRO_SESSION = "shiro-session:";
    private static final int SESSION_VAL_TIME_SPAN = 1800;

    // 保存到Redis中key的前缀 prefix+sessionId
    private String redisShiroSessionPrefix = REDIS_SHIRO_SESSION;

    // 设置会话的过期时间
    private int redisShiroSessionTimeout = SESSION_VAL_TIME_SPAN;

    @Autowired
    private TokenApi tokenApi;

    public void saveSession(Session session,String sessionid) {
        if (session == null)
            throw new NullPointerException("session is empty");
        try {
            ShiroSessionModel tokenModel=new ShiroSessionModel();
            tokenModel.setShiro_session(SerializationUtils.serialize(session));
            tokenModel.setShiro_session_id(sessionid);
            tokenApi.saveSession(tokenModel);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("save session error");
        }
    }
    public void updateSession(Session session) {
        if (session == null || session.getId() == null)
            throw new NullPointerException("session is empty");
        try {
            ShiroSessionModel tokenModel=new ShiroSessionModel();
            tokenModel.setShiro_session(SerializationUtils.serialize(session));
            tokenModel.setShiro_session_id(session.getId().toString());
            tokenApi.updateSession(tokenModel);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("save session error");
        }
    }

    public void refreshSession(String sessionId) {
        //tokenApi.refreshSession(sessionId);
    }

    public void deleteSession(String id) {
        if (id == null) {
            throw new NullPointerException("session id is empty");
        }
        try {

            tokenApi.deleteSession(id);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("delete session error");
        }
    }

    public Session getSession(String id) {
        if (id == null)
            throw new NullPointerException("session id is empty");
        Session session = null;
        try {
            ShiroSessionModel tokenModel=tokenApi.getSession(id);
            session=(Session) SerializationUtils.deserialize(tokenModel.getShiro_session());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("get session error");
        }
        return session;
    }

    public Collection<Session> getAllSessions() {
        System.out.println("get all sessions");
        return null;
    }

    private String buildRedisSessionKey(Serializable sessionId) {
        return redisShiroSessionPrefix + sessionId;
    }


}